package com.rongmei.jdsync.dto.sync;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

import java.math.BigDecimal;
import java.util.List;

/**
 * 价格同步选项配置
 *
 * @author dongjun
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Schema(description = "价格同步选项配置")
public class SyncOptions {

    @Schema(description = "价格变更阈值（元），小于此值的变更将被忽略", example = "0.01")
    private BigDecimal priceThreshold;

    @Schema(description = "批量处理大小", example = "50")
    private int batchSize;

    @Schema(description = "是否强制同步（忽略阈值）", example = "false")
    private boolean forceSync;

    @Schema(description = "指定同步的商品ID列表，如果为空则从第三方系统获取")
    private List<Long> specificGoodsIds;

    @Schema(description = "是否跳过京东数据获取，直接使用本地数据", example = "false")
    private boolean skipJdDataFetch;

    @Schema(description = "同步模式：FULL-全量同步，INCREMENTAL-增量同步", example = "FULL")
    private SyncMode syncMode;

    @Schema(description = "最大重试次数", example = "3")
    private int maxRetryCount;

    @Schema(description = "是否异步执行", example = "false")
    private boolean asyncExecution;

    /**
     * 同步模式枚举
     */
    public enum SyncMode {
        FULL,        // 全量同步
        INCREMENTAL  // 增量同步
    }

    /**
     * 创建默认同步选项
     */
    public static SyncOptions createDefault() {
        return SyncOptions.builder()
                .priceThreshold(new BigDecimal("0.01"))
                .batchSize(1000)
                .forceSync(false)
                .skipJdDataFetch(false)
                .syncMode(SyncMode.FULL)
                .maxRetryCount(3)
                .asyncExecution(false)
                .build();
    }

    /**
     * 创建强制同步选项
     */
    public static SyncOptions createForceSync() {
        return SyncOptions.builder()
                .priceThreshold(BigDecimal.ZERO)
                .batchSize(1000)
                .forceSync(true)
                .skipJdDataFetch(false)
                .syncMode(SyncMode.FULL)
                .maxRetryCount(3)
                .asyncExecution(false)
                .build();
    }

    /**
     * 创建指定商品同步选项
     */
    public static SyncOptions createSpecificGoods(List<Long> goodsIds) {
        return SyncOptions.builder()
                .priceThreshold(new BigDecimal("0.01"))
                .batchSize(1000)
                .forceSync(false)
                .specificGoodsIds(goodsIds)
                .skipJdDataFetch(false)
                .syncMode(SyncMode.FULL)
                .maxRetryCount(3)
                .asyncExecution(false)
                .build();
    }

    /**
     * 验证选项配置的有效性
     */
    public boolean isValid() {
        return batchSize > 0 && 
               maxRetryCount >= 0 && 
               priceThreshold != null && 
               priceThreshold.compareTo(BigDecimal.ZERO) >= 0;
    }

    /**
     * 获取有效的批量大小
     */
    public int getEffectiveBatchSize() {
        return Math.max(batchSize, 1);
    }

    /**
     * 是否有指定的商品ID
     */
    public boolean hasSpecificGoods() {
        return specificGoodsIds != null && !specificGoodsIds.isEmpty();
    }
}
